
/*

NOTES

Replication do-file for publication in World Development for 
		Resource rents, coercion, and local development: 
			Evidence from post-apartheid South Africa

					Paulo Bastos 	(World Bank)
					Nicolas Bottan 	(Cornell University)

This do-file generates Tables 4 to 11, and Tables A2 to A4
					
*/


* Set WD
cd ""

* Set Output Directory
global results "results/tables/"


use data/WD_panel_census2011_centroid.dta, clear




*****************************
*** 1996-2011
*****************************
cap drop aux
egen t=group(year)
xtset spcode t

gen district=substr(saux,1,3)

* Initial population as weight
gen weight=totpop if year==1996
egen aux=max(weight), by(spcode)
replace weight=aux
drop aux

gen ltotpop=log(totpop)


*** Prepare controls

* Fillin X Y
foreach var in X Y {
	gen aux=`var' if year==1996
	egen aux2 = max(aux), by(spcode)
	replace `var'=aux2
	drop aux aux2
	}

* Age groups
gen age_1 = cage0_4 + cage5_9
gen age_2 = cage10_14 + cage15_19
egen age_3 = rowtotal(cage20_24-cage35_39)
egen age_4 = rowtotal(cage40_44-cage55_59)
egen age_5 = rowtotal(cage60_64-cage85)

* Piped water
replace water_piped_pipedex_public = water_piped+water_pipedex+water_public if year==1996
gen pipedwater= water_piped_pipedex_public

rename educ_none edu_none

* Generate interactions w Homeland for controls
foreach var in dist pcincome pcincome_i_mean race_white race_black race_indasian race_colored age_1 age_2 age_3 age_4 age_5 hedu_none hedu_incprimary hedu_primary hedu_incsecond hedu_second hedu_higher employed male light_elgrid refuse_none pipedwater totpop employed_mining employed_manufacture mar_married mar_widow mar_divorced u_employed_mining u_employed_manufacture  edu_none edu_incprimary edu_primary edu_incsecond edu_second edu_higher lpcincome lpcincome_i_mean ltotpop {
	gen H`var' = homeland*`var'
	}

* 1996 controls
local controls1 "dist race_white race_indasian race_colored age_2 age_3 age_4 age_5 hedu_incprimary hedu_primary hedu_incsecond hedu_second hedu_higher employed male pcincome totpop light_elgrid refuse_none pipedwater"

local Hcontrols1 "Hdist Hrace_white Hrace_indasian Hrace_colored Hage_2 Hage_3 Hage_4 Hage_5 Hhedu_incprimary Hhedu_primary Hhedu_incsecond Hhedu_second Hhedu_higher Hemployed Hmale Hpcincome Htotpop Hlight_elgrid Hrefuse_none Hpipedwater"

* 2011 controls
local controls2 "f.race_white f.race_indasian f.race_colored f.age_2 f.age_3 f.age_4 f.age_5 f.edu_incprimary f.edu_primary f.edu_incsecond f.edu_second f.edu_higher f.employed f.male f.totpop f.light_elgrid f.refuse_none f.pipedwater"

local Hcontrols2 "f.Hrace_white f.Hrace_indasian f.Hrace_colored f.Hage_2 f.Hage_3 f.Hage_4 f.Hage_5 f.Hedu_incprimary f.Hedu_primary f.Hedu_incsecond f.Hedu_second f.Hedu_higher f.Hemployed f.Hmale f.Htotpop f.Hlight_elgrid f.Hrefuse_none f.Hpipedwater"


*** Define dep var
xtset spcode t
local depvar df.lpcincome
local indepvars "homeland employed_mining Hemployed_mining"
local placebo "employed_manufacture Hemployed_manufacture"


* Cluster by district/former state
egen cluslevel=group(district name)


************************
************************
************************
*** MAIN REGRESSIONS ***
************************
************************
************************

* Table 4: Relation between initial share of employment in mining and growth in per capita income (1996-2011)

xi: reg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r cluster(cluslevel)
	outreg2 using ${results}table4.xls, keep(`indepvars')  excel nocons bracket bdec(3) replace
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(prcode) cluster(cluslevel)
	outreg2 using ${results}table4.xls, keep(`indepvars')  excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table4.xls, keep(`indepvars')  excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' `controls2' `Hcontrols2' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table4.xls, keep(`indepvars')  excel nocons bracket bdec(3) append


* Estimate magnitude
qui: sum employed_mining if homeland, det
local d99=r(p99)-r(p50)
local d95=r(p95)-r(p50)
qui xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight], r abs(prcode) cluster(cluslevel)
nlcom _b[employed_mining]*`d95'+_b[Hemployed_mining]*`d95'
nlcom _b[employed_mining]*`d99'+_b[Hemployed_mining]*`d99'




* Table 5: Robustness to sample selection

xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=10, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table5.xls, keep(`indepvars') excel nocons bracket bdec(3) replace
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=15, r abs(district) cluster(cluslevel)
	outreg2  using ${results}table5.xls, keep(`indepvars') excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2  using ${results}table5.xls, keep(`indepvars') excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=60, r abs(district) cluster(cluslevel)
	outreg2  using ${results}table5.xls, keep(`indepvars') excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight], r abs(district) cluster(cluslevel)
	outreg2 using ${results}table5.xls, keep(`indepvars') excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' `controls1' [w=weight] if homeland==1, r abs(district) cluster(cluslevel)
	outreg2  using ${results}table5.xls, keep(employed_mining) excel nocons bracket bdec(3) append





* Table 6: Robustness to spatial controls
gen distsq=dist^2
gen Hdistsq=homeland*distsq
gen distcu=dist^3
gen Hdistcu=homeland*distcu
local rd1 "distsq distcu Hdistsq Hdistcu"

preserve
keep if dist<30
bspline, xvar(dist2) knots(-30 -25 -15 -5 5 15 25 30) gen(bs) power(3)

gen XY=X*Y
local rd3 "X Y XY"
gen X2=X^2
gen Y2=Y^2
gen XY2=XY^2
gen X3=X^3
gen Y3=Y^3
gen XY3=XY^3
local rd4 "X Y XY X2 Y2 XY2 X3 Y3 XY3"


xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table6.xls, keep(`indepvars') excel nocons bracket bdec(3) replace
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' `rd1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table6.xls, keep(`indepvars') excel nocons bracket bdec(3) append
xi: reg `depvar' `indepvars'  `controls1' `Hcontrols1' bs* [w=weight] if dist<=30, r cluster(cluslevel)
	outreg2 using ${results}table6.xls, keep(`indepvars') excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' `rd3' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table6.xls, keep(`indepvars') excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' `rd4' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table6.xls, keep(`indepvars') excel nocons bracket bdec(3) append

restore




* Table 7: Access to infrastructure

xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' f.light_elgrid f.Hlight_elgrid [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table7.xls, keep(`indepvars' light_elgrid Hlight_elgrid F1.light_elgrid F1.Hlight_elgrid) excel nocons bracket bdec(3) replace
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' f.refuse_none f.Hrefuse_none [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table7.xls, keep(`indepvars' refuse_none Hrefuse_none F1.refuse_none F1.Hrefuse_none) excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' f.pipedwater f.Hpipedwater [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table7.xls, keep(`indepvars' pipedwater Hpipedwater F1.pipedwater F1.Hpipedwater) excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' f.light_elgrid f.Hlight_elgrid f.refuse_none f.Hrefuse_none f.pipedwater f.Hpipedwater [w=weight] if dist<=30, r abs(district)  cluster(cluslevel)
	outreg2 using ${results}table7.xls, keep(`indepvars' light_elgrid Hlight_elgrid F1.light_elgrid F1.Hlight_elgrid refuse_none Hrefuse_none F1.refuse_none F1.Hrefuse_none pipedwater Hpipedwater F1.pipedwater F1.Hpipedwater) excel nocons bracket bdec(3) append







* Table 8: Access to markets and connectivity

* Province capitals
/*
Province	Capital	Coordinates
Eastern Cape	Bhisho	-32.853333, 27.436389
Free State	Bloemfontein	-29.116111, 26.214167
Gauteng	Johannesburg	-26.206111, 28.04
KwaZulu-Natal	Pietermaritzburg	-29.601389, 30.382222
Limpopo	Polokwane	-23.9125, 29.455833
Mpumalanga	Nelspruit	-25.4375, 30.971944
North West	Mafikeng	-25.834167, 25.611667
Northern Cape	Kimberley	-28.736111, 24.768889
Western Cape	Cape Town	-33.924167, 18.418611l
*/

* Largest cities (2011)
/*

Name	Population	Latitude/Longitude
1	Cape Town  , Western Cape	3,433,441	-33.926 / 18.423
2	Durban  , KwaZulu-Natal	3,120,282	-29.858 / 31.029
3	Johannesburg  , Gauteng	2,026,469	-26.202 / 28.044
4	Soweto  , Gauteng	1,695,047	-26.268 / 27.858
5	Pretoria  , Gauteng	1,619,438	-25.745 / 28.188
6	Port Elizabeth  , Eastern Cape	967,677	-33.918 / 25.57
7	Pietermaritzburg  , KwaZulu-Natal	750,845	-29.617 / 30.393
8	Benoni  , Gauteng	605,344	-26.188 / 28.321
9	Tembisa, Gauteng	511,655	-25.996 / 28.227
10	East London  , Eastern Cape	478,676	-33.015 / 27.912
*/

matrix input LAT1=(-33.926, -29.858, -26.202, -26.268, -25.745, -33.918, -29.617, -26.188, -25.996, -33.015)
matrix input LON1=(18.423, 31.029, 28.044, 27.858, 28.188, 25.57, 30.393, 28.321, 28.227, 27.912)
matrix input POP=(3433441, 3120282, 2026469, 1695047, 1619438, 967677, 750845, 605344, 511655, 478676)

forvalues i=1(1)10 {
	local a=LAT1[1,`i']
	local b=LON1[1,`i']
	vincenty Y X `a' `b' , l(city`i') inkm
	}
egen city_min=rowmin(city1-city10)
egen city_median=rowmedian(city1-city10)
egen city_average=rowmean(city1-city10)
egen city_p30=rowpctile(city1-city10), p(30)
gen city_waverage=0.23*city1 + 0.21*city2 + 0.13*city3 + 0.11*city4 + 0.11*city5 + 0.06*city6 + 0.05*city7 + 0.04*city8 + 0.03*city9 + 0.03*city10

gen Hcity_waverage=city_waverage*homeland
gen Hcity_min=city_min*homeland


forvalues i=1(1)3 {
	gen lmpchange`i'=log(mp`i'_pcincome_i_zeros)-log(pre_mp`i'_pcincome_i_zeros)
	}

local r=2
gen b4mp= pre_mp`r'_pcincome_i_zeros
gen lmpchange=lmpchange`r'

gen Hdistroad = homeland * distroad

xi: areg `depvar' `indepvars' city_waverage Hcity_waverage  `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table8.xls, keep(`indepvars' city_waverage Hcity_waverage) excel nocons bracket bdec(3) replace
xi: areg `depvar' `indepvars' distroad Hdistroad `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table8.xls, keep(`indepvars' distroad Hdistroad) excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' city_waverage Hcity_waverage distroad Hdistroad `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table8.xls, keep(`indepvars' city_waverage Hcity_waverage distroad Hdistroad) excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' city_waverage Hcity_waverage distroad Hdistroad f.light_elgrid f.Hlight_elgrid f.refuse_none f.Hrefuse_none f.pipedwater f.Hpipedwater `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table8.xls, keep(`indepvars' city_waverage Hcity_waverage distroad Hdistroad) excel nocons bracket bdec(3) append







* Table 9: Population growth, 1996-2011

xi: reg df.ltotpop `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r cluster(cluslevel)
	outreg2 using ${results}table9.xls, keep(`indepvars') excel nocons bracket bdec(3) replace

xi: areg df.ltotpop `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(prcode) cluster(cluslevel)
	outreg2 using ${results}table9.xls, keep(`indepvars') excel nocons bracket bdec(3) append
	
xi: areg df.ltotpop `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table9.xls, keep(`indepvars') excel nocons bracket bdec(3) append

xi: areg df.ltotpop `indepvars' `controls1' `Hcontrols1' `controls2' `Hcontrols2' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table9.xls, keep(`indepvars') excel nocons bracket bdec(3) append

	
	
	
* Table 10: Change in population age structure, 1996-2011	
local k=0
forvalues w=1(1)5 {	
	local ++k
	gen auxage=age_`w'
	xi: areg df.auxage `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
		if `k'==1 {
		outreg2 using ${results}table10.xls, keep(`indepvars') excel nocons bracket bdec(3) replace
		}
		else {
		outreg2 using ${results}table10.xls, keep(`indepvars') excel nocons bracket bdec(3) append
		}
	drop auxage
	}


	
	
	
	
* Table 11: Other dependent variables, 1996-2011

replace edu_second=hedu_second if edu_second==.
replace edu_higher=hedu_higher if edu_higher==.
gen highed=edu_second+edu_higher
gen dedu=f.highed-highed

gen dlight=f.light_elgrid-light_elgrid
gen ddwel = (f.dwel_shackby + f.dwel_shack) - (dwel_shackby + dwel_shack)
gen dwater = (f.water_piped_pipedex_public) - (water_piped + water_pipedex + water_public)
gen dnoref = f.refuse_none - refuse_none


* Determinants of education growth

* 1996 controls
local controls1 "dist race_white race_indasian race_colored age_2 age_3 age_4 age_5  employed male pcincome totpop light_elgrid refuse_none pipedwater"
local Hcontrols1 "Hdist Hrace_white Hrace_indasian Hrace_colored Hage_2 Hage_3 Hage_4 Hage_5  Hemployed Hmale Hpcincome Htotpop Hlight_elgrid Hrefuse_none Hpipedwater"

xi: areg dedu `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table11.xls, keep(`indepvars') excel nocons bracket bdec(3) replace

	
* Determinants of electric grid growth

* 1996 controls
local controls1 "dist race_white race_indasian race_colored age_2 age_3 age_4 age_5 hedu_incprimary hedu_primary hedu_incsecond hedu_second hedu_higher employed male pcincome totpop refuse_none pipedwater"
local Hcontrols1 "Hdist Hrace_white Hrace_indasian Hrace_colored Hage_2 Hage_3 Hage_4 Hage_5 Hhedu_incprimary Hhedu_primary Hhedu_incsecond Hhedu_second Hhedu_higher Hemployed Hmale Hpcincome Htotpop Hrefuse_none Hpipedwater"
	
xi: areg dlight `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table11.xls, keep(`indepvars') excel nocons bracket bdec(3) append


* Determinants of no refuse growth

* 1996 controls
local controls1 "dist race_white race_indasian race_colored age_2 age_3 age_4 age_5 hedu_incprimary hedu_primary hedu_incsecond hedu_second hedu_higher employed male pcincome totpop light_elgrid  pipedwater"
local Hcontrols1 "Hdist Hrace_white Hrace_indasian Hrace_colored Hage_2 Hage_3 Hage_4 Hage_5 Hhedu_incprimary Hhedu_primary Hhedu_incsecond Hhedu_second Hhedu_higher Hemployed Hmale Hpcincome Htotpop Hlight_elgrid  Hpipedwater"

xi: areg dnoref `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table11.xls, keep(`indepvars') excel nocons bracket bdec(3) append
	

* Determinants of piped water growth

* 1996 controls
local controls1 "dist race_white race_indasian race_colored age_2 age_3 age_4 age_5 hedu_incprimary hedu_primary hedu_incsecond hedu_second hedu_higher employed male pcincome totpop light_elgrid refuse_none"
local Hcontrols1 "Hdist Hrace_white Hrace_indasian Hrace_colored Hage_2 Hage_3 Hage_4 Hage_5 Hhedu_incprimary Hhedu_primary Hhedu_incsecond Hhedu_second Hhedu_higher Hemployed Hmale Hpcincome Htotpop Hlight_elgrid Hrefuse_none"

xi: areg dwater `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}table11.xls, keep(`indepvars') excel nocons bracket bdec(3) append

	

	
	
	
	

**** Appendix

* Program to prepare data
cap: program drop prepdata

program define prepdata

cap drop aux
egen t=group(year)
xtset spcode t

gen district=substr(saux,1,3)

* Initial population as weight
gen weight=totpop if year==1996
egen aux=max(weight), by(spcode)
replace weight=aux
drop aux

gen ltotpop=log(totpop)


*** Prepare controls

* Age groups
gen age_1 = cage0_4 + cage5_9
gen age_2 = cage10_14 + cage15_19
egen age_3 = rowtotal(cage20_24-cage35_39)
egen age_4 = rowtotal(cage40_44-cage55_59)
egen age_5 = rowtotal(cage60_64-cage85)

* Piped water
replace water_piped_pipedex_public = water_piped+water_pipedex+water_public if year==1996
gen pipedwater= water_piped_pipedex_public

rename educ_none edu_none

* Generate interactions w Homeland for controls
foreach var in dist pcincome pcincome_i_mean race_white race_indasian race_colored age_1 age_2 age_3 age_4 age_5 hedu_none hedu_incprimary hedu_primary hedu_incsecond hedu_second hedu_higher employed male light_elgrid refuse_none pipedwater totpop employed_mining employed_manufacture mar_married mar_widow mar_divorced u_employed_mining u_employed_manufacture  edu_none edu_incprimary edu_primary edu_incsecond edu_second edu_higher lpcincome lpcincome_i_mean ltotpop {
	gen H`var' = homeland*`var'
	}

*** Define dep var
xtset spcode t
local depvar df.lpcincome
local indepvars "homeland employed_mining Hemployed_mining"

egen cluslevel=group(district name)

end



* Table A3) Robustness to different community matching definitions


matrix define A1=J(4,9,.)

use data/WD_panel_census2011_centroid.dta, clear
prepdata

* 1996 controls
local controls1 "dist race_white race_indasian race_colored age_2 age_3 age_4 age_5 hedu_incprimary hedu_primary hedu_incsecond hedu_second hedu_higher employed male pcincome totpop light_elgrid refuse_none pipedwater"

local Hcontrols1 "Hdist Hrace_white Hrace_indasian Hrace_colored Hage_2 Hage_3 Hage_4 Hage_5 Hhedu_incprimary Hhedu_primary Hhedu_incsecond Hhedu_second Hhedu_higher Hemployed Hmale Hpcincome Htotpop Hlight_elgrid Hrefuse_none Hpipedwater"

* 2011 controls
local controls2 "f.race_white f.race_indasian f.race_colored f.age_2 f.age_3 f.age_4 f.age_5 f.edu_incprimary f.edu_primary f.edu_incsecond f.edu_second f.edu_higher f.employed f.male f.totpop f.light_elgrid f.refuse_none f.pipedwater"

local Hcontrols2 "f.Hrace_white f.Hrace_indasian f.Hrace_colored f.Hage_2 f.Hage_3 f.Hage_4 f.Hage_5 f.Hedu_incprimary f.Hedu_primary f.Hedu_incsecond f.Hedu_second f.Hedu_higher f.Hemployed f.Hmale f.Htotpop f.Hlight_elgrid f.Hrefuse_none f.Hpipedwater"


*** Define dep var
xtset spcode t
local depvar df.lpcincome
local indepvars "homeland employed_mining Hemployed_mining"
local placebo "employed_manufacture Hemployed_manufacture"


xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}tableA3.xls, keep(`indepvars') excel nocons bracket bdec(3) replace
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' `controls2' `Hcontrols2' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}tableA3.xls, keep(`indepvars') excel nocons bracket bdec(3) append


	

use data/WD_panel_census2011_areaunique.dta, clear
prepdata
egen aux=max(nrsp), by(spcode)
replace nrsp=aux
drop aux


* 1996 controls
local controls1 "dist race_white race_indasian race_colored age_2 age_3 age_4 age_5 hedu_incprimary hedu_primary hedu_incsecond hedu_second hedu_higher employed male pcincome totpop light_elgrid refuse_none pipedwater"

local Hcontrols1 "Hdist Hrace_white Hrace_indasian Hrace_colored Hage_2 Hage_3 Hage_4 Hage_5 Hhedu_incprimary Hhedu_primary Hhedu_incsecond Hhedu_second Hhedu_higher Hemployed Hmale Hpcincome Htotpop Hlight_elgrid Hrefuse_none Hpipedwater"

* 2011 controls
local controls2 "f.race_white f.race_indasian f.race_colored f.age_2 f.age_3 f.age_4 f.age_5 f.edu_incprimary f.edu_primary f.edu_incsecond f.edu_second f.edu_higher f.employed f.male f.totpop f.light_elgrid f.refuse_none f.pipedwater"

local Hcontrols2 "f.Hrace_white f.Hrace_indasian f.Hrace_colored f.Hage_2 f.Hage_3 f.Hage_4 f.Hage_5 f.Hedu_incprimary f.Hedu_primary f.Hedu_incsecond f.Hedu_second f.Hedu_higher f.Hemployed f.Hmale f.Htotpop f.Hlight_elgrid f.Hrefuse_none f.Hpipedwater"


*** Define dep var
xtset spcode t
local depvar df.lpcincome
local indepvars "homeland employed_mining Hemployed_mining"
local placebo "employed_manufacture Hemployed_manufacture"



xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}tableA3.xls, keep(`indepvars') excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' `controls2' `Hcontrols2' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}tableA3.xls, keep(`indepvars') excel nocons bracket bdec(3) append



keep if nrsp==1
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}tableA3.xls, keep(`indepvars') excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' `controls2' `Hcontrols2' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}tableA3.xls, keep(`indepvars') excel nocons bracket bdec(3) append






* Table A4: Robustness to imputing missing income responses by community mean

use data/WD_panel_census2011_centroid.dta, clear
prepdata



* 1996 controls
local controls1 "dist race_white race_indasian race_colored age_2 age_3 age_4 age_5 hedu_incprimary hedu_primary hedu_incsecond hedu_second hedu_higher employed male pcincome totpop light_elgrid refuse_none pipedwater"

local Hcontrols1 "Hdist Hrace_white Hrace_indasian Hrace_colored Hage_2 Hage_3 Hage_4 Hage_5 Hhedu_incprimary Hhedu_primary Hhedu_incsecond Hhedu_second Hhedu_higher Hemployed Hmale Hpcincome Htotpop Hlight_elgrid Hrefuse_none Hpipedwater"

* 2011 controls
local controls2 "f.race_white f.race_indasian f.race_colored f.age_2 f.age_3 f.age_4 f.age_5 f.edu_incprimary f.edu_primary f.edu_incsecond f.edu_second f.edu_higher f.employed f.male f.totpop f.light_elgrid f.refuse_none f.pipedwater"

local Hcontrols2 "f.Hrace_white f.Hrace_indasian f.Hrace_colored f.Hage_2 f.Hage_3 f.Hage_4 f.Hage_5 f.Hedu_incprimary f.Hedu_primary f.Hedu_incsecond f.Hedu_second f.Hedu_higher f.Hemployed f.Hmale f.Htotpop f.Hlight_elgrid f.Hrefuse_none f.Hpipedwater"


*** Define dep var
xtset spcode t


local indepvars "homeland employed_mining Hemployed_mining"
local depvar df.lpcincome_i_mean

xi: reg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r cluster(cluslevel)
	outreg2 using ${results}tableA4.xls, keep(`indepvars') excel nocons bracket bdec(3) replace
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(prcode) cluster(cluslevel)
	outreg2 using ${results}tableA4.xls, keep(`indepvars') excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}tableA4.xls, keep(`indepvars') excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' `controls2' `Hcontrols2' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}tableA4.xls, keep(`indepvars') excel nocons bracket bdec(3) append

	
	
	
	
	
	

* Table A5: Robustness to using unconditional employment in mining

local depvar df.lpcincome

local indepvars "homeland u_employed_mining Hu_employed_mining"

xi: reg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r cluster(cluslevel)
	outreg2 using ${results}tableA5.xls, keep(`indepvars') excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(prcode) cluster(cluslevel)
	outreg2 using ${results}tableA5.xls, keep(`indepvars') excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}tableA5.xls, keep(`indepvars') excel nocons bracket bdec(3) append
xi: areg `depvar' `indepvars' `controls1' `Hcontrols1' `controls2' `Hcontrols2' [w=weight] if dist<=30, r abs(district) cluster(cluslevel)
	outreg2 using ${results}tableA5.xls, keep(`indepvars') excel nocons bracket bdec(3) append




	
	
	
	
	
* Table A2: Descriptive Statistics, community-level, regressions data


use data/WD_panel_census2011_centroid.dta, clear


*****************************
*** 1996-2011
*****************************
cap drop aux
egen t=group(year)
xtset spcode t

gen district=substr(saux,1,3)

* Initial population as weight
gen weight=totpop if year==1996
egen aux=max(weight), by(spcode)
replace weight=aux
drop aux

gen ltotpop=log(totpop)


*** Prepare controls

* Fillin X Y
foreach var in X Y {
	gen aux=`var' if year==1996
	egen aux2 = max(aux), by(spcode)
	replace `var'=aux2
	drop aux aux2
	}

* Age groups
gen age_1 = cage0_4 + cage5_9
gen age_2 = cage10_14 + cage15_19
egen age_3 = rowtotal(cage20_24-cage35_39)
egen age_4 = rowtotal(cage40_44-cage55_59)
egen age_5 = rowtotal(cage60_64-cage85)

* Piped water
replace water_piped_pipedex_public = water_piped+water_pipedex+water_public if year==1996
gen pipedwater= water_piped_pipedex_public

rename educ_none edu_none

* Generate interactions w Homeland for controls
foreach var in dist pcincome pcincome_i_mean race_white race_indasian race_colored age_1 age_2 age_3 age_4 age_5 hedu_none hedu_incprimary hedu_primary hedu_incsecond hedu_second hedu_higher employed male light_elgrid refuse_none pipedwater totpop employed_mining employed_manufacture mar_married mar_widow mar_divorced u_employed_mining u_employed_manufacture  edu_none edu_incprimary edu_primary edu_incsecond edu_second edu_higher lpcincome lpcincome_i_mean ltotpop {
	gen H`var' = homeland*`var'
	}

* 1996 controls
local controls1 "dist race_white race_indasian race_colored age_2 age_3 age_4 age_5 hedu_incprimary hedu_primary hedu_incsecond hedu_second hedu_higher employed male pcincome totpop light_elgrid refuse_none pipedwater"

local Hcontrols1 "Hdist Hrace_white Hrace_indasian Hrace_colored Hage_2 Hage_3 Hage_4 Hage_5 Hhedu_incprimary Hhedu_primary Hhedu_incsecond Hhedu_second Hhedu_higher Hemployed Hmale Hpcincome Htotpop Hlight_elgrid Hrefuse_none Hpipedwater"

* 2011 controls
local controls2 "f.race_white f.race_indasian f.race_colored f.age_2 f.age_3 f.age_4 f.age_5 f.edu_incprimary f.edu_primary f.edu_incsecond f.edu_second f.edu_higher f.employed f.male f.totpop f.light_elgrid f.refuse_none f.pipedwater"

local Hcontrols2 "f.Hrace_white f.Hrace_indasian f.Hrace_colored f.Hage_2 f.Hage_3 f.Hage_4 f.Hage_5 f.Hedu_incprimary f.Hedu_primary f.Hedu_incsecond f.Hedu_second f.Hedu_higher f.Hemployed f.Hmale f.Htotpop f.Hlight_elgrid f.Hrefuse_none f.Hpipedwater"


*** Define dep var
xtset spcode t
local depvar df.lpcincome
local indepvars "homeland employed_mining Hemployed_mining"
local placebo "employed_manufacture Hemployed_manufacture"


* Cluster by district/former state
egen cluslevel=group(district name)

replace pcincome=pcincome/1000
replace pcincome_i_mean=pcincome_i_mean/1000



postfile descr str20 var str12 hl str12 nhl using ${results}TableA2_desc_96.dta, replace

foreach var in pcincome pcincome_i_mean employed_mining u_employed_mining employed_manufacture employed_agriculture employed_construction employed_wholesale employed_socserv race_black race_white race_indasian race_colored age_2 age_3 age_4 age_5 hedu_incprimary hedu_primary hedu_incsecond hedu_second hedu_higher employed male light_elgrid refuse_none pipedwater {

	sum `var' if year==1996 & homeland==1 & dist<30 [w=totpop]
	local m1 = round(r(mean),.001)
	local s1 = round(r(sd),.001)

	sum `var' if year==1996 & homeland==0 & dist<30 [w=totpop]
	local m2 = round(r(mean),.001)
	local s2 = round(r(sd),.001)

	post descr ("`var'") ("`m1'") ("`m2'")
	post descr (" ") ("[`s1']") ("[`s2']")

	}

	
	local var "totpop"
	
	sum `var' if year==1996 & homeland==1 & dist<30
	local m1 = round(r(mean),.001)
	local s1 = round(r(sd),.001)

	sum `var' if year==1996 & homeland==0 & dist<30
	local m2 = round(r(mean),.001)
	local s2 = round(r(sd),.001)

	post descr ("`var'") ("`m1'") ("`m2'")
	post descr (" ") ("[`s1']") ("[`s2']")

postclose descr


postfile descr str20 var str12 hl str12 nhl using ${results}TableA2_desc_11.dta, replace

foreach var in pcincome pcincome_i_mean race_black race_white race_indasian race_colored age_2 age_3 age_4 age_5 edu_incprimary edu_primary edu_incsecond edu_second edu_higher employed male pcincome light_elgrid refuse_none pipedwater {

	sum `var' if year==2011 & homeland==1 & dist<30 [w=totpop]
	local m1 = round(r(mean),.001)
	local s1 = round(r(sd),.001)

	sum `var' if year==2011 & homeland==0 & dist<30 [w=totpop]
	local m2 = round(r(mean),.001)
	local s2 = round(r(sd),.001)

	post descr ("`var'") ("`m1'") ("`m2'")
	post descr (" ") ("[`s1']") ("[`s2']")

	}

	
	local var "totpop"
	
	sum `var' if year==2011 & homeland==1 & dist<30
	local m1 = round(r(mean),.001)
	local s1 = round(r(sd),.001)

	sum `var' if year==2011 & homeland==0 & dist<30
	local m2 = round(r(mean),.001)
	local s2 = round(r(sd),.001)

	post descr ("`var'") ("`m1'") ("`m2'")
	post descr (" ") ("[`s1']") ("[`s2']")

postclose descr




	





